package com.huawei.hwphy2d;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Handler;
import android.view.Choreographer;
import android.view.Choreographer.FrameCallback;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import com.huawei.hwphy2d.physical.config.json.Config;
import com.huawei.hwphy2d.physical.config.json.Config.ActorBean.ImagesBean;
import com.huawei.hwphy2d.physical.config.json.Config.StageBean.BarriersBean;
import com.huawei.hwphy2d.physical.model.Barrier;
import com.huawei.hwphy2d.physical.model.Barrier.ORIENTATION;
import com.huawei.hwphy2d.physical.model.HwBody;
import com.huawei.hwphy2d.physical.model.Size;
import com.huawei.hwphy2d.physical.util.BarrierParameters;
import com.huawei.hwphy2d.physical.util.DeviceParameters;
import com.huawei.hwphy2d.physical.util.RandomRange;
import com.huawei.hwphy2d.view.HwPhysicalView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import o.bqq;
import o.fzp;
import o.gac;

public class HwPhysicalSystem implements FrameCallback {
    private static final String TAG = HwPhysicalSystem.class.getSimpleName();
    public static final long TIME_STAMP_INTERVAL = 16;
    public static final int VIEW_TYPE_SURFACE_VIEW = 1;
    public static final int VIEW_TYPE_VIEW = 2;
    private int activeCount = 0;
    private List<HwBody> activeList = new ArrayList();
    private List<HwBody> bodyList;
    private HwPhysicalView hwPhysicalView;
    private int imgCount = 0;
    private boolean isPlay = false;
    private Barrier mBarrier;
    private List<Barrier> mBarriers = null;
    private Config mConfig;
    private Context mContext;
    private long mCurrentTime = 0;
    private Handler mHandler = new Handler();
    private String mParticlePath;
    private int mPosition = 30;
    private float mRandSizeRate = 1.0f;
    private ViewGroup mRootView;
    private Size mScreenSize;
    PhysicalUpdateTask mUpdateTask;
    private int mVelocity = 15;
    private int mViewType;
    private float mWindPreX = 0.0f;
    private int[] mWindowSize = new int[2];
    private gac mWorld;
    private float mWorldRate = 0.016666668f;
    private int releaseCount = 0;
    private long sleepId = 0;
    private int sprayNum = 10;

    class PhysicalUpdateTask implements Runnable {
        private final Handler mHandler;
        private final WeakReference<HwPhysicalSystem> mPhysicalSystem;

        public PhysicalUpdateTask(HwPhysicalSystem hwPhysicalSystem, Handler handler) {
            this.mPhysicalSystem = new WeakReference(hwPhysicalSystem);
            this.mHandler = handler;
        }

        public void run() {
            HwPhysicalSystem hwPhysicalSystem = (HwPhysicalSystem) this.mPhysicalSystem.get();
            if (hwPhysicalSystem != null) {
                hwPhysicalSystem.onUpdate(hwPhysicalSystem.mCurrentTime);
                hwPhysicalSystem.mCurrentTime = hwPhysicalSystem.mCurrentTime + 16;
                if (this.mHandler != null) {
                    this.mHandler.postDelayed(this, 16);
                }
            }
        }
    }

    public HwPhysicalSystem(ViewGroup viewGroup, int i, String str, String str2) {
        this.mRootView = viewGroup;
        this.mViewType = i;
        this.mContext = this.mRootView.getContext();
        this.mParticlePath = str2;
        this.mScreenSize = DeviceParameters.getWindowPixels(this.mContext);
        if (!(this.mViewType == 1 || this.mViewType == 2)) {
            this.mViewType = 2;
        }
        this.mWindowSize[0] = (int) this.mScreenSize.getWidth();
        this.mWindowSize[1] = (int) this.mScreenSize.getHeight();
        this.mConfig = Config.getConfig(this.mContext, str);
        for (ImagesBean imagesBean : this.mConfig.getActor().getImages()) {
            this.imgCount = imagesBean.getCount() + this.imgCount;
        }
        this.mWorld = new gac(new fzp((float) this.mConfig.getStage().getGravity_field().getX(), (float) this.mConfig.getStage().getGravity_field().getY()));
        this.sprayNum = this.mConfig.getActor().getEmission_rate();
        BarrierParameters.RATIO = (float) this.mConfig.getStage().getGravity_field().getAxis_ratio();
        this.mWorldRate = (float) this.mConfig.getStage().getGravity_field().getMock_world_rate();
        this.mVelocity = this.mConfig.getStage().getGravity_field().getVelocity();
        this.mPosition = this.mConfig.getStage().getGravity_field().getPosition();
        this.mBarriers = new ArrayList();
        this.mUpdateTask = new PhysicalUpdateTask(this, this.mHandler);
        initPhysicalSystem();
    }

    public void initPhysicalSystem() {
        createHorizontalBounds();
        createVerticalBounds();
        createBarrier();
        initActorList();
    }

    private void createHorizontalBounds() {
        this.mBarrier = new Barrier(this.mConfig, this.mWorld, this.mScreenSize);
        if (this.mConfig.getStage().getBorder().isLeft()) {
            this.mBarrier.createRoundBounds(50.0f, 0.0f, new fzp((float) this.mWindowSize[0], (float) this.mWindowSize[1]), 0, ORIENTATION.ORIENTATION_LEFT_RIGHT, "left");
        }
        if (this.mConfig.getStage().getBorder().isRight()) {
            this.mBarrier.createRoundBounds(50.0f, 0.0f, new fzp((float) this.mWindowSize[0], (float) this.mWindowSize[1]), 0, ORIENTATION.ORIENTATION_LEFT_RIGHT, "right");
        }
    }

    private void createVerticalBounds() {
        this.mBarrier = new Barrier(this.mConfig, this.mWorld, this.mScreenSize);
        if (this.mConfig.getStage().getBorder().isTop()) {
            this.mBarrier.createRoundBounds(0.0f, 50.0f, new fzp(0.0f, (float) (-this.mWindowSize[1])), 0, ORIENTATION.ORIENTATION_TOP_BOTTOM, "top");
        }
        if (this.mConfig.getStage().getBorder().isBottom()) {
            this.mBarrier.createRoundBounds(0.0f, 50.0f, new fzp(0.0f, (float) (this.mWindowSize[1] - 18)), 0, ORIENTATION.ORIENTATION_TOP_BOTTOM, "bottom");
        }
    }

    private void createBarrier() {
        for (BarriersBean barriersBean : this.mConfig.getStage().getBarriers()) {
            this.mBarrier = new Barrier(this.mConfig, this.mWorld, this.mScreenSize);
            this.mBarrier.createRoundBounds((float) barriersBean.getWidth(), (float) barriersBean.getHeight(), new fzp((float) barriersBean.getX(), (float) barriersBean.getY()), barriersBean.getAngle(), ORIENTATION.ORIENTATION_NONE, null);
            this.mBarriers.add(this.mBarrier);
        }
    }

    private void initActorList() {
        this.bodyList = new ArrayList(this.imgCount);
        this.activeList = new ArrayList();
        for (ImagesBean imagesBean : this.mConfig.getActor().getImages()) {
            Bitmap bitmap;
            Bitmap bitmap2 = null;
            try {
                File file = new File(this.mParticlePath + imagesBean.getUrl());
                if (file.exists()) {
                    bitmap2 = BitmapFactory.decodeStream(new FileInputStream(file));
                }
                bitmap = bitmap2;
            } catch (FileNotFoundException e) {
                bqq.ॱ.ˊ(TAG, e.toString());
                bitmap = null;
            }
            int count = imagesBean.getCount();
            for (int i = 0; i < count; i++) {
                this.mRandSizeRate = ((Double) imagesBean.getScale().get(RandomRange.getNextRandomInt(0, imagesBean.getScale().size()))).floatValue();
                HwBody hwBody = new HwBody(this.mWorld, this.mRandSizeRate, this.mContext);
                hwBody.build(bitmap, this.mConfig);
                this.bodyList.add(hwBody);
            }
        }
        Collections.shuffle(this.bodyList);
    }

    public void doFrame(long j) {
        if (this.mWorld != null) {
            this.mWorld.ˋ(this.mWorldRate, this.mVelocity, this.mPosition);
        }
        Choreographer.getInstance().postFrameCallback(this);
    }

    public void onUpdate(long j) {
        windBlowing();
        while (this.bodyList != null && !this.bodyList.isEmpty() && ((float) this.activeCount) < ((float) (((long) this.sprayNum) * j)) / 1000.0f) {
            HwBody hwBody = (HwBody) this.bodyList.remove(0);
            hwBody.getBody().ॱ(true);
            this.activeList.add(hwBody);
            this.activeCount++;
        }
        if (this.activeList != null) {
            for (HwBody hwBody2 : this.activeList) {
                if (!hwBody2.inViewPoint(new Rect(0, 0, this.mWindowSize[0], (int) (((float) this.mWindowSize[1]) + (hwBody2.getHeight() * 2.0f)))) && hwBody2.isActive()) {
                    hwBody2.setActive(false);
                    this.releaseCount++;
                }
            }
        }
        if (this.hwPhysicalView != null) {
            this.hwPhysicalView.invalidate();
        }
    }

    private float windBlowing() {
        if (this.mConfig.getStage().getGravity_field().isWind()) {
            if (this.mWorld != null && this.sleepId >= 150) {
                int nextRandomInt = RandomRange.getNextRandomInt(1, 3);
                if (nextRandomInt == 1) {
                    this.mWindPreX = -10.0f;
                } else if (nextRandomInt == 2) {
                    this.mWindPreX = 10.0f;
                } else {
                    this.mWindPreX = 0.0f;
                }
                this.mWorld.ˎ(new fzp(this.mWindPreX, this.mWorld.ˏ().ॱ));
                this.sleepId = 0;
            }
            this.sleepId++;
        }
        return this.mWindPreX;
    }

    public void setGravity(float f, float f2) {
        fzp o_fzp = new fzp(f, this.mWorld.ˏ().ॱ);
        if (this.mWorld != null) {
            this.mWorld.ˎ(o_fzp);
        }
    }

    public void play() {
        if (!(this.mViewType == 1 || this.mViewType == 2)) {
            this.mViewType = 2;
        }
        if (this.hwPhysicalView != null) {
            this.mRootView.removeView(this.hwPhysicalView);
            this.hwPhysicalView = null;
        }
        if (this.hwPhysicalView == null && this.mViewType == 2) {
            this.hwPhysicalView = new HwPhysicalView(this.mContext);
            this.mRootView.addView(this.hwPhysicalView, new LayoutParams(-1, -1));
        }
        this.hwPhysicalView.setDebug_draw(this.mConfig.getStage().isDebug_draw());
        this.hwPhysicalView.setBarriers(this.mBarriers);
        this.hwPhysicalView.setActiveList(this.activeList);
        this.mHandler.post(this.mUpdateTask);
        Choreographer.getInstance().postFrameCallback(this);
        this.isPlay = true;
    }

    public void stop() {
        if (!(this.mHandler == null || this.mUpdateTask == null)) {
            this.mHandler.removeCallbacks(this.mUpdateTask);
        }
        this.mRootView.removeView(this.hwPhysicalView);
        this.hwPhysicalView = null;
        this.activeCount = 0;
        this.mCurrentTime = 0;
        this.releaseCount = 0;
        if (this.mWorld != null) {
            this.mWorld.ˎ();
        }
        if (this.bodyList != null) {
            for (HwBody release : this.bodyList) {
                release.release();
            }
            this.bodyList.clear();
            this.bodyList = null;
        }
        if (this.activeList != null) {
            for (HwBody release2 : this.activeList) {
                release2.release();
            }
            this.activeList.clear();
            this.activeList = null;
        }
        this.isPlay = false;
    }

    public void destroy() {
        if (this.bodyList != null) {
            for (HwBody hwBody : this.bodyList) {
                if (hwBody.getBody() != null) {
                    hwBody.release();
                }
            }
            this.bodyList.clear();
            this.bodyList = null;
        }
        if (this.activeList != null) {
            for (HwBody hwBody2 : this.activeList) {
                if (hwBody2.getBody() != null) {
                    hwBody2.release();
                }
            }
            this.activeList.clear();
            this.activeList = null;
        }
    }

    public HwPhysicalView getHwPhysicalView() {
        return this.hwPhysicalView;
    }

    public boolean isPlay() {
        return this.isPlay;
    }
}
